﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;

using FCNS.KeepMoney.Data;
using FCNS.KeepMoney.Configuration;

namespace FCNS.KeepMoney.Util
{
    class ExchangeUtil
    {
        public static void DownloadExchange(List<ExchangeField> es)
        {
            WebClient web = new WebClient();
            string str = System.Text.Encoding.Default.GetString(
                web.DownloadData("http://data.bank.hexun.com/ll/rmbhl.aspx"));

            web.Dispose();
            if (str.Length < 100)
                return;

            //<div class="dzMain">
            //</table>
            try
            {
                int start = str.IndexOf("<div class=\"dzMain\">");
                str = str.Remove(0, start);

                start = str.IndexOf("<tr");
                int end = str.IndexOf("</tr") + 6;
                string one = str.Substring(start, end - start);
                str = str.Remove(0, end);

                start = str.IndexOf("<tr");
                end = str.IndexOf("</tr");
                string two = str.Substring(start, end - start);

                es.Clear();
                string[] strOne = one.Split(new string[] { "</th>" }, StringSplitOptions.RemoveEmptyEntries);
                string[] strTwo = two.Split(new string[] { "</td>" }, StringSplitOptions.RemoveEmptyEntries);

                AppDefines.exchangeDay= strTwo[0].Remove(0, strTwo[0].LastIndexOf(">") + 1);
                for (int i = 1; i < strTwo.Length; i++)
                {
                    es.Add(new ExchangeField(strOne[i].Remove(0, strOne[i].LastIndexOf(">") + 1), "",
                        strTwo[i].Remove(0, strTwo[i].LastIndexOf(">") + 1)));
                }
            }
            catch { }
        }
    }

}
